package com.dhcc.sdc.busi.controller;


import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.SqlRunner;
import com.dhcc.cdc.busi.service.ICdcHospitalService;
import com.dhcc.core.framework.annotion.Permission;
import com.dhcc.core.framework.util.Bean2MapUtil;
import com.dhcc.core.framework.util.DateUtil;
import com.dhcc.core.modules.system.warpper.LogWarpper;
import com.dhcc.sds.base.service.ISdsCommonService;
import com.dhcc.sds.base.service.ISdsQcentityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;



import com.dhcc.sdc.busi.entity.SdcBusiOperationLogEntity;
import com.dhcc.sdc.busi.service.ISdcBusiOperationLogService;


/**
 * 单病种过程质控操作日志
 *
 * @ClassName: SdcBusiOperationLogController
 * @Description: TODO
 * @author sunyingjie
 * @date 2023-03-06 10:55:54
 */
@Controller
@RequestMapping("${sys.backendPath}/sdc/sdcbusioperationlog")
public class SdcBusiOperationLogController extends BaseController {

	private String PREFIX = "/backend/sdc/log/";

	@Autowired
	private ISdcBusiOperationLogService sdcBusiOperationLogService;
    @Autowired
    private ICdcHospitalService cdcHospitalService;
    @Autowired
    private ISdsCommonService sdsCommonService;
    @Autowired
    private ISdsQcentityService sdsQcentityService;

    /**
     * 跳转到单病种过程质控操作日志列表首页
     */
    @RequestMapping("")
    public String index(@RequestParam Map<String, Object> params, Model model) {
        if (CommonUtil.isNotEmpty(params.get("key"))) {
            String key= params.get("key").toString();
            Date now = new Date();
            String beginTime = "", endTime = "";
            if (key.equals("todayCount")) {
                beginTime = endTime = DateUtil.getDay(now);
            } else if (key.equals("thisMonthCount")) {
                beginTime = DateUtil.getMinMonthDate(now);
                endTime = DateUtil.getMaxMonthDate(now);
            } else if (key.equals("thisYearCount")) {
                beginTime = DateUtil.getYear(now)+"-01-01";
                endTime = DateUtil.getYear(now)+"-12-31";
            }

            model.addAttribute("beginTime", beginTime);
            model.addAttribute("endTime", endTime);
        }
        if (CommonUtil.isNotEmpty(params.get("logType"))) {
            model.addAttribute("logType", params.get("logType"));
        }
        if (CommonUtil.isNotEmpty(params.get("hospital"))) {
            model.addAttribute("hospitalbak", params.get("hospital"));
        }
        if (CommonUtil.isNotEmpty(params.get("depart"))) {
            model.addAttribute("departbak", params.get("depart"));
        }
        if (CommonUtil.isNotEmpty(params.get("qcentity"))) {
            model.addAttribute("qcentitybak", params.get("qcentity"));
        }
        if (CommonUtil.isNotEmpty(params.get("userName"))) {
            model.addAttribute("userName", params.get("userName"));
        }
        if (CommonUtil.isNotEmpty(params.get("medicalGroup"))) {
            model.addAttribute("medicalGroupbak", params.get("medicalGroup"));
        }
        //获取部门信息
        JSONArray officMap = cdcHospitalService.getOfficeMap();
        //加载用户关联科室
        JSONArray departMap = sdsCommonService.getDepartMap(new HashMap<>());
        //获取病种
        JSONArray entityMap = sdsQcentityService.getUpEntityMap();
        //日志类型集合
        JSONObject logTypeMap = sdcBusiOperationLogService.logTypeMap();
        model.addAttribute("officeMap", officMap);
        model.addAttribute("departMap", departMap);
        model.addAttribute("entityMap", entityMap);
        model.addAttribute("logTypeMap", logTypeMap);
        return PREFIX + "sdcbusioperation_log.html";
    }

    /**
     * 获取单病种过程质控操作日志列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam Map<String, Object> params) {
        // 查询登录用户关联科室
        List<String> deptIds = sdsCommonService.getDeptIdsBySysUser();
        params.put("userLinkDepts", deptIds);
		Page<SdcBusiOperationLogEntity> page = new PageFactory<SdcBusiOperationLogEntity>().defaultPage();
        List<SdcBusiOperationLogEntity> result = this.sdcBusiOperationLogService.page(page,params);
        page.setRecords((List<SdcBusiOperationLogEntity>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 查询操作日志详情
     */
    @RequestMapping("/detail/{id}")
    @Permission("operation:log:detail")
    @ResponseBody
    public Object detail(@PathVariable Long id) {
        SdcBusiOperationLogEntity operationLog = this.sdcBusiOperationLogService.selectById(id);
        Map<String, Object> stringObjectMap = Bean2MapUtil.toMap(operationLog);
        return super.warpObject(new LogWarpper(stringObjectMap));
    }

    /**
     * 删除单病种过程质控操作日志
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete() {
        SqlRunner.db().delete("delete from t_sdc_busi_operation_log");
        return R.ok();
    }

}
